package cn.gailvlun.gll.presentation.apartment.room;

import android.app.Activity;
import android.app.Dialog;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityIPromiseBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.Member;
import cn.gailvlun.gll.net.apartment.member.ModifyMemberReq;
import cn.gailvlun.gll.presentation.apartment.room.vm.IPromiseViewModel;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.functions.Consumer;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;



public class IPromiseActivity extends BaseActivity {

    public static void openActivity(Activity activity, int memberId, Consumer<Result<Activity>> consumer) {
        Intent intent = new Intent(activity, IPromiseActivity.class);
        intent.putExtra("memberId", memberId);
        RxActivityResult.on(activity)
                .startIntent(intent)
                .subscribe(consumer);
    }

    private ActivityIPromiseBinding mBinding;
    private IPromiseViewModel mViewModel;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_i_promise);
        mBinding.setPresenter(new Presenter());
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        super.onBackPressed();
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        int memberId = getIntent().getIntExtra("memberId", -1);
        if (memberId == -1) {
            finish();
        }
        mViewModel = ViewModelProviders.of(this).get(IPromiseViewModel.class);
        mViewModel.getMemberById(memberId);
    }

    public class Presenter {
        public void onPromise() {
            final Dialog progressDialog = DialogUtil.showIndeterminate(mContext);
            HttpMethods.getMemberService()
                    .modifyMember(ModifyMemberReq.isPromise(mViewModel.getMemberId(), true))
                    .compose(RxUtil.<Member>applyScheduler())
                    .compose(IPromiseActivity.this.<Member>bindToLifecycle())
                    .subscribe(new HttpSubscriber<Member>() {
                        @Override
                        protected void onSuccess(final Member response) {
                            
                            mViewModel.saveMember(response);
                            setResult(RESULT_OK);
                            finish();
                        }

                        @Override
                        protected void onFailure(String errMsg, @Nullable Member response, int code) {
                            DialogUtil.showError(mContext, errMsg);
                        }

                        @Override
                        protected void onFinish() {
                            super.onFinish();
                            progressDialog.dismiss();
                        }
                    });
        }
    }
}
